<?php
/**
 * User: i@fedax.cn
 * Time: 2018/9/6 18:50
 */

namespace app\api\model;


use think\Model;
use Yansongda\Pay\Pay;

class WxPay extends Model
{
    protected $config = [
//        'appid' => 'wx0a3144f6153c4690', // APP APPID
        'app_id' => 'wx0a3144f6153c4690', // 公众号 APPID
//        'miniapp_id' => 'wxb3fxxxxxxxxxxx', // 小程序 APPID
        'mch_id' => '1494804512',
        'key' => '5f1885178fc4d8ed6dbe0341ded7d74b',
        'notify_url' => 'https://api.quniuyuyin.com/Api/Public/wx_notify',
//        'cert_client' => './cert/apiclient_cert.pem', // optional，退款等情况时用到
//        'cert_key' => './cert/apiclient_key.pem',// optional，退款等情况时用到
//        'log' => [ // optional
//            'file' => './logs/wechat.log',
//            'level' => 'debug'
//        ],
        'mode' => 'optional', // 当为 `hk` 时，为香港 gateway 当为dev时进入沙箱模式。
    ];

    public function index()
    {
        $order = [
            'out_trade_no' => time(),
            'body' => 'subject-测试',
            'total_fee' => '1',
        ];

        $pay = Pay::wechat($this->config)->wap($order);

        // $pay->appId
        // $pay->timeStamp
        // $pay->nonceStr
        // $pay->package
        // $pay->signType
    }

    public function notify()
    {
        $pay = Pay::wechat($this->config);

        try {
            $data = $pay->verify(); // 是的，验签就这么简单！

            \think\facade\Log::error($data->all());
        } catch (\Exception $e) {
            $e->getMessage();
        }

        return $pay->success()->send();// laravel 框架中请直接 `return $pay->success()`
    }
}